package com.lebang.service.sys;

import com.lebang.domain.CencesReqAdCount;
import com.lebang.domain.PkgBlackLog;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.example.PkgBlackLogExample;
import com.lebang.mapper.CencesReqAdCountMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.mapper.PkgBlackLogMapper;
import com.lebang.model.enums.buss.UserStatus;
import com.lebang.service.BaseService;
import com.lebangweb.common.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * pkg_black_log
 *
 * @Author hwq
 * @Date 2020-07-21
 */
@Service
public class PkgBlackLogService extends BaseService<PkgBlackLog, Integer, PkgBlackLogExample> {
    @Autowired
    private PkgBlackLogMapper pkgBlackLogMapper;
    @Autowired
    private LbbMemberMapper lbbMemberMapper;
    @Autowired
    private CencesReqAdCountMapper cencesReqAdCountMapper;

    public void addLog(String s, RequestHeaders requestHeaders, String remark) {
        if(TextUtil.isEmpty(requestHeaders.getUserid())){
            return;
        }
        if (pkgBlackLogMapper.check(s, requestHeaders.getUserid(), requestHeaders.getImei()) == null) {
            PkgBlackLog pkgBlackLog = new PkgBlackLog();
            pkgBlackLog.setCreateTime(LocalDateTime.now());
            pkgBlackLog.setModifyTime(LocalDateTime.now());
            pkgBlackLog.setHasDeleted(0);
            pkgBlackLog.setRemark(remark);
            pkgBlackLog.setMemberId(requestHeaders.getUserid());
            pkgBlackLog.setOtherId(s);
            pkgBlackLog.setImei(requestHeaders.getImei());
            this.insertModel(pkgBlackLog);
            LbbMember member=lbbMemberMapper.selectById(requestHeaders.getUserid());
            if(member!=null){
                member.setStatus(UserStatus.FIXED.name());
                lbbMemberMapper.updateByPrimaryKeySelective(member);
            }
        }
    }


    public Integer pkgBlackLogService(RequestHeaders requestHeaders) {
        if(TextUtil.isEmpty(requestHeaders.getUserid())){
           requestHeaders.setUserid(null);
        }
        if(TextUtil.isEmpty(requestHeaders.getUserid())&&TextUtil.isEmpty(requestHeaders.getOldImei())){
            return null;
        }
        return pkgBlackLogMapper.checkFail(requestHeaders.getUserid(),requestHeaders.getOldImei());
    }

    public void deleteLog(String memberId, String imei,String otherId) {
        PkgBlackLog pkgBlackLog = pkgBlackLogMapper.selectOne(memberId,imei,otherId);
        if(pkgBlackLog!=null){
            pkgBlackLogMapper.deleteByPrimaryKey(pkgBlackLog.getId());
        }
    }

}
